import axios from 'axios';
import router from '/@/router/index';
let service = axios.create({
  timeout: 30000,
});

service.interceptors.request.use(
  (config) => {
    let token = sessionStorage.getItem('token');
    if (token) {
      config.headers['Token'] = token;
    } else {
      // router4.0
      if (router.currentRoute.value.name != 'login') {
        router.push({
          path: '/',
        });
        return Promise.reject();
      }
    }
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject();
  },
);

service.interceptors.response.use(
  (response) => {
    if (response.data.code == 200) {
      // 接口正常请求成功
      return response.data;
    } else {
      // 接口返回错误
      return response.data;
    }
  },
  (error) => {
    console.log(error.data);
    return Promise.reject();
  },
);

export default service;
